package hander

import (
	"context"
	"go.uber.org/zap"
	"good_srv/config"
	"good_srv/internal/model/dao"
	"good_srv/pkg"
	__ "good_srv/proto"
	"math/rand"
	"strconv"
)

// 定义空接口
type OrderService struct {
	__.UnimplementedOrderServiceServer
}

// 实现方法
func (s *OrderService) OrderAdd(ctx context.Context, req *__.OrderAddRequest) (resp *__.OrderAddResponse, err error) {
	var good dao.Goods
	config.DB.Where("id=?", req.UserId).Limit(1).Find(&good)
	if good.ID == 0 {
		zap.S().Error("该商品不存在")
		return
	}
	if good.GoodsNum < int(req.GoodsNum) {
		zap.S().Error("该商品库存不充足")
		return
	}
	orders := dao.Orders{
		OrderSn:     strconv.Itoa(rand.Intn(10)),
		UserId:      1,
		OrderStatus: 0,
		PayStatus:   0,
		GoodsName:   good.GoodsName,
		GoodsPrice:  good.GoodsPrice,
		GoodsImg:    good.GoodsImg,
		GoodsNum:    good.GoodsNum,
		OrderAmount: good.GoodsPrice * float64(req.GoodsNum),
	}
	config.DB.Create(&orders)
	var payUrl = ""
	payUrl = pkg.AliPay("9021000142616592", "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC4B+VKPL4dOeygl6yjgxgQrNJI1COidNRYxPdJJujw6MIE/quh22oxmKsXeoOnTKbG9acrRwpytkcGSfgvvEPH3fDwwmVFTs2jpjiaGTs94kjGLAy8lIVOTnZmcTlBXTWtTuLctRAny3BrGrR7IXNKqQftN19veWZe5gtGLvt+rBIcEfjIJhlKC5yasUQviTQVZcJ1btZ8aabPXpa8Bl4VnvETLO3T55tXB+Q12yUgn1e3JYwv/rukPrvNJz2j+7fV8fXYkRO2Wq4b8G3DESzLpE3urCY2E6Aopn/ptZ1EdgKh7rEDiFWraDVOixE9VsqnhIkfYwdqPnlUH5Y75PIrAgMBAAECggEBAKX/YUDMd8RPGnVlOvEm788ixG+zAw2oCl6kFFakxVuA6Hmu6kwOacSg7Ds6ZITE7G7s6MRtySvd5UTz9e3BCPbA6HOTNdwFkBI8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xQxmLJ0oXpzMPidK6JGCFAjcTfJQc=", "https://localhost", string(req.GoodsId), string(req.UserId), string(req.GoodsNum))
	//tx := config.DB.Model(&good).Update("order_status", 1)
	//if tx.Error != nil {
	//	zap.S().Error("修改订单状态失败")
	//	return
	//}
	//update := config.DB.Model(&good).Update("goods_num", good.GoodsNum-int(req.GoodsNum))
	//if update.Error != nil {
	//	zap.S().Error("商品库存同步失败")
	//	return
	//}
	zap.S().Error("支付完成")
	return &__.OrderAddResponse{Url: payUrl}, nil
}
